package com.todod.controller;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestAttribute;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.todod.entity.QueryEntry;
import com.todod.entity.ResultData;
import com.todod.mapper.MdTemplateMapper;
import com.todod.model.MdTemplate;
import com.todod.model.User;
import com.todod.service.IMdTemplateService;
import com.todod.service.IUserService;

@RestController
@RequestMapping("/mdTemplate")
public class MdTemplateController {
	@Autowired
	private IMdTemplateService mdTemplateService;
	@Autowired
	private MdTemplateMapper mdTemplateMapper;
	@Autowired
	private IUserService iUserService;

	/**
	 * 
	 * @param companyId
	 * @param _userId
	 * @param request
	 * @param queryProjectInfo
	 * @return
	 */
	@RequestMapping("/getListPage")
	public ResultData getListPage(@RequestAttribute Long _userId, HttpServletRequest request,
			@RequestBody QueryEntry queryEntry) {

		IPage<MdTemplate> page = mdTemplateService.getListPage(_userId, queryEntry);
		return ResultData.success("ok", page);
	}

	/**
	 * 
	 * @param _userId
	 * @param reqCatalogManage
	 * @return
	 */
	@PostMapping("/save")
	public ResultData saveData(@RequestAttribute Long _userId, @RequestBody MdTemplate mdTemplate) {

		mdTemplateService.saveData(_userId, mdTemplate);
		return ResultData.success();
	}

	/**
	 * 
	 * @param _userId
	 * @param reqCatalogManage
	 * @return
	 */
	@PostMapping("/edit")
	public ResultData editData(@RequestAttribute Long _userId, @RequestBody MdTemplate mdTemplate) {
		mdTemplateService.editData(_userId, mdTemplate);
		return ResultData.success();
	}

	/**
	 * 
	 * @param code
	 * @return
	 */
	@GetMapping("/getInfo")
	public ResultData getInfo(Long code) {
		if (code == null) {
			return ResultData.error(ResultData.PARAM_ERROR_CODE, "参数错误");
		}

		MdTemplate mdTemplate = mdTemplateMapper.selectById(code);
		return ResultData.success("ok", mdTemplate);
	}

	/**
	 * 
	 * @param ids
	 * @return
	 */
	@RequestMapping("/updateState")
	public ResultData updateState(@RequestAttribute Long _userId, Long code, String status) {
		mdTemplateService.updateState(_userId, code, status);
		return ResultData.success();
	}

	/**
	 * 获取默认数据
	 * 
	 * @Title: onDefault
	 * @Description:
	 * @author gsh
	 * @date 2019年7月3日
	 * @param session
	 * @param id
	 * @return
	 */
	@PostMapping("/getDefaultData")
	public MdTemplate getDefaultData(@RequestAttribute Long _userId, String type) {

		User user = iUserService.getById(_userId);

		MdTemplate byDefaultData = mdTemplateService.getByDefaultDataByType(type, user.getDepartid());

		return byDefaultData;
	}

	/**
	 * 
	 * @param ids
	 * @return
	 */
	@RequestMapping("/onDefault")
	public ResultData onDefault(@RequestAttribute Long _userId, Long code) {
		mdTemplateService.onDefault(_userId, code);
		return ResultData.success();
	}

}
